# collector.py
import psutil
import time
import threading

# 全局数据存储（环形队列）
DATA = {
    'cpu': [],
    'max_len': 3600  # 最多存1小时数据，1秒一条
}

def collect_data():
    while True:
        cpu_percent = psutil.cpu_percent(interval=1)
        if len(DATA['cpu']) >= DATA['max_len']:
            DATA['cpu'].pop(0)  # 超出则删除最旧
        DATA['cpu'].append(cpu_percent)

def start_collector():
    thread = threading.Thread(target=collect_data, daemon=True)
    thread.start()
